<template>
  <div class="system-user-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div class="system-user-search mb15">
        <el-input size="default" placeholder="手机号备注名称" style="max-width: 180px"
                  v-model="state.tableData.param.keyword"></el-input>
        <el-button size="default" type="primary" class="ml10" @click="searchRow">
          <el-icon>
            <ele-Search/>
          </el-icon>
          查询
        </el-button>
        <el-button size="default" type="success" class="ml10" @click="onOpenAddUser('add')">
          <el-icon>
            <ele-FolderAdd/>
          </el-icon>
          新增用户
        </el-button>
      </div>
      <el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
        <el-table-column prop="id" label="序号" width="60"/>
        <el-table-column prop="name" label="名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="phone" label="手机号" show-overflow-tooltip></el-table-column>
        <el-table-column prop="unpayOrderNum" label="未支付订单数" show-overflow-tooltip></el-table-column>
        <el-table-column prop="unpaidOrderAmount" label="未支付订单金额" show-overflow-tooltip></el-table-column>
        <el-table-column prop="remake" label="备注" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" text type="primary"
                       @click="onOpenEditUser('edit', scope.row)"
            >修改
            </el-button
            >
            <el-button size="small" text type="primary"
                       @click="onRowDel(scope.row)">删除
            </el-button>
            <el-button size="small" text type="primary"
                       @click="lookDetileRow(scope.row)">查看记录
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
          class="mt15"
          :pager-count="5"
          :page-sizes="[10, 20, 30]"
          v-model:current-page="state.tableData.param.pageNum"
          background
          v-model:page-size="state.tableData.param.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.total"
      >
      </el-pagination>
    </el-card>
    <UserDialog ref="userDialogRef" @refresh="getTableData()"/>
  </div>
</template>

<script setup lang="ts" name="systemUser">
import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
import {getUserList, delUser} from '/@/api/user/user/index'
import {ElMessageBox, ElMessage} from 'element-plus';
import {delJing, getJingList} from "/@/api/jd/jing";

import {useRouter} from 'vue-router';

// 引入组件
const UserDialog = defineAsyncComponent(() => import('/@/views/user/user/dialog.vue'));

// 定义变量内容
const userDialogRef = ref();
const router = useRouter();
const state = reactive<SysUserState>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      keyword: '',
      pageNum: 1,
      pageSize: 10,
    },
  },
});

// 初始化表格数据
const getTableData = async () => {
  const data = await getUserList(state.tableData.param);
  state.tableData.loading = true;
  state.tableData.data = data.data.rows;
  state.tableData.total = data.data.total;
  setTimeout(() => {
    state.tableData.loading = false;
  }, 1000);
};
// 打开新增用户弹窗
const onOpenAddUser = (type: string) => {
  userDialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditUser = (type: string, row: RowUserType) => {
  userDialogRef.value.openDialog(type, row);
};
// 删除用户
const onRowDel = (row: RowUserType) => {
  ElMessageBox.confirm(`此操作将永久删除名称：“${row.name}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(() => {

        const param = {id: row.id}
        delUser(param)
        setTimeout(() => {
          state.tableData.loading = false;
        }, 1000);
        getTableData();
        ElMessage.success('删除成功');
      })
      .catch(() => {
      });
};
const lookDetileRow = (row: RowUserType) => {
  const params: EmptyObjectType = {id: row.id};
  router.push({
    path: '/user/details',
    query: params,
  });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});

const searchRow = () => {
  getTableData();
};

</script>

<style scoped lang="scss">
.system-user-container {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;

    .el-table {
      flex: 1;
    }
  }
}
</style>
